<template>
  <div class="register">
      <el-form 
        :model="ruleForm" 
        status-icon 
        :rules="rules" 
        ref="ruleForm" 
        label-width="70px" 
        class="register-box">
        <h3 class="title">用户注册</h3>
        <el-form-item label="用户名" prop="phone">
          <el-input type="text" v-model="ruleForm.phone" autocomplete="off" placeholder="用户名"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off" placeholder="密码"></el-input>
        </el-form-item>

        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" placeholder="确认密码"></el-input>
        </el-form-item>

        <el-form-item label="姓名" prop="name">
          <el-input type="text" v-model="ruleForm.name" autocomplete="off" placeholder="姓名"></el-input>
        </el-form-item>

        <el-form-item label="用户角色" prop="role">
           <el-select v-model="ruleForm.role" placeholder="请选择用户角色">
              <el-option
                v-for="item in rolelist"
                :key="item._id"
                :label="item.name"
                :value="item._id">
              </el-option>
            </el-select>
        </el-form-item>

        <el-form-item class="form-footer">
          <el-button type="primary" @click="submitForm()">注册</el-button>
        </el-form-item>
      </el-form>
  </div>
</template>

<script>
import {user_register,role_list} from '../utils/api'

export default {
  data(){
    var validatePhone= (rule, value, callback) => {
      if ( !/^1[3-9]\d{9}$/.test(value) ) {
        callback(new Error('请输入正确的手机号'));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if ( !/^\d{4}$/.test(value) ) {
        callback(new Error('请输入正确的密码'));
      } else {
        callback();
      }
    };
    var validateCheckPass = (rule, value, callback) => {
      if ( value !== this.ruleForm.pass ) {
        callback(new Error('两次输入密码不一致'));
      } else {
        callback();
      }
    };
    var validateName = (rule, value, callback) => {
      if ( value.length < 2 ) {
        callback(new Error('请输入正确的姓名'));
      } else {
        callback();
      }
    };
    var validateRole = (rule, value, callback) => {
      if ( !value ) {
        callback(new Error('请选择一个角色'));
      } else {
        callback();
      }
    };

    return {
      //角色列表
      rolelist:[],
      ruleForm: {
        phone: '', 
        pass: '', 
        checkPass: '', 
        name: '', 
        role: '',
      },
      rules: {
        phone: [

          { validator: validatePhone, trigger: 'blur' }
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validateCheckPass, trigger: 'blur' }
        ],
        name: [
          { validator: validateName, trigger: 'blur' }
        ],
        role: [
          { validator: validateRole, trigger: 'blur' }
        ],
      }
    }
  },
  mounted(){
    role_list().then((res)=>{
      this.rolelist = res.data.list;
    })
  },
  methods:{
    submitForm(){
     
      this.$refs.ruleForm.validate((valid)=>{
        if( valid ){
          
          var {checkPass,...params} = this.ruleForm;

          //发起注册请求.
          user_register(params).then((res)=>{
            if( res.data.code == 200 ){
              this.$message({ message:res.data.msg,type:'success' });
              
            }else{
              this.$message({ message:res.data.msg,type:'error' });
            }
          })

        }else{
          console.log('验证失败');

        }
      })
    }
  }
}
</script>

<style lang='scss'>
.register{
  height: 100%;
  background: url(../assets/考勤管理-登录页-背景.png) no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .title{
    text-align: center;
    padding-bottom: 30px;
  }
  .register-box{
    background-color: white;
    padding: 30px;
    border-radius: 10px;
    min-width: 400px;
    .form-footer{
      display: flex;
      justify-content: center;
    }
  }
}
</style>